import {I18Parse,CommonLang} from "@lowcode/jpaas-common-lib";

var i18LangUtil = {
    jsFiles :require.context('@/locales/lang/impl', true, /\.js$/).keys()
};

/**
 * 根据语言将数据格式为:
 * {form:{
 *          zh-CN:”表单"
 *    }
 * }
 * 转换为:
 * {form:”表单"}
 * @param langKey
 * @returns {{}}
 */
i18LangUtil.langeFilesHandler =function(langKey){
    let obj ={};
    let commonLang=I18Parse.langeHandler(CommonLang,langKey);
    obj = { ...obj,...commonLang };

    i18LangUtil.jsFiles.forEach(file => {
        var name = file.substring(2, file.lastIndexOf('.'))+".js";
        var jsObj = require(`@/locales/lang/impl/${name}`);
        let json =jsObj.default;
        let jsonLang=I18Parse.langeHandler(json,langKey);
        obj = { ...obj,...jsonLang };
    })
    return obj;
}

export default i18LangUtil;
